package com.link.base.base.signin.service;

import com.link.base.base.signin.dao.mybatis.mapper.SignInMapper;
import com.link.base.base.signin.model.SignIn;
import com.link.base.base.signin.model.SignInDetails;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author unknown
 * @date unknown
 * @comments 签到
 */
@Service
public class SignInServiceImpl extends BasicServiceImpl<SignIn> implements SignInService {

    @Resource
    private SignInMapper signInMapper;
    @Resource
    private SignInDetailsService signInDetailsService;

    @Override
    public BasicMapper<SignIn> getBasicMapper() {
        return signInMapper;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void currentUserSign(Long userId) throws Exception {
        if (null == userId) {
            throw new ServiceException("PUBLIC-016");
        }
        //当前系统日期时间
        Date nowDate = new Date();
        //设置日期格式 'yyyy-MM-dd HH:mm:ss'
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将当前系统日期时间转换为字符串
        String currentDateTime = df.format(nowDate);

        SignIn signIn = queryByUserId(userId);
        //如果找不到该用户的签到头表记录，表示该用户首次签到，则新建头记录
        if (signIn == null) {
            signIn = new SignIn();
            signIn.setSignUserId(userId);
            signIn.setSignInDays(new Double(0));
            signIn.setAccountSignDays(new Double(0));
            signIn.setLastSignTime(null);
            //新建时【兑换记录】字段的默认值
            String exchangeRecords = "无记录";
            signIn.setExchangeRecords(exchangeRecords);
            insert(signIn);
        }
        //校验能否签到
        if (ableSignIn(currentDateTime, signIn.getLastSignTime()) == false) {
            throw new ServiceException("SIGNIN-002");
        }
        //新建签到明细
        Long signInId = signIn.getId();
        SignInDetails signInDetails = new SignInDetails();
        signInDetails.setHeadId(signInId);
        //设置日期格式 'yyyy-MM-dd HH:mm:ss'
        SimpleDateFormat dtf = new SimpleDateFormat("yyyy-MM-dd");
        //将当前系统日期转换为字符串
        String currentDate = dtf.format(nowDate);
        signInDetails.setSignDate(currentDate);
        //默认为Y
        signInDetails.setSignInStatus("Y");
        signInDetailsService.insert(signInDetails);
        //统计并更新【签到总天数】
        signInDaysUpdate(signInId);
        //更新【最后签到日期】
        signIn.setLastSignTime(currentDateTime);
        signInMapper.lastSignTimeUpdate(signIn);
    }

    @Override
    public boolean ableSignIn(String nowDate, String lastSignDate) {
        if (StringUtils.isBlank(nowDate)) {
            throw new ServiceException("SIGNIN-003");
        }
        if (StringUtils.isBlank(lastSignDate)) {
            return true;
        }
        //日期格式
        String dateType = "yyyy-MM-dd";
        nowDate = nowDate.substring(0, dateType.length());
        lastSignDate = lastSignDate.substring(0, dateType.length());
        if (StringUtils.equals(nowDate, lastSignDate)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean ableSignIn(Long userId) throws Exception {
        //能否签到
        boolean ableSignInFlag = false;
        SignIn signIn = queryByUserId(userId);
        String lastSignTime = null;
        if (signIn != null) {
            lastSignTime = signIn.getLastSignTime();
        }
        //当前系统日期时间
        Date nowDate = new Date();
        //日期格式 'yyyy-MM-dd HH:mm:ss'
        String dateType = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat df = new SimpleDateFormat(dateType);
        //将当前系统日期时间转换为字符串
        String currentDateTime = df.format(nowDate);
        ableSignInFlag = ableSignIn(currentDateTime, lastSignTime);
        return ableSignInFlag;
    }

    @Override
    public SignIn queryByUserId(Long userId) throws Exception {
        if (null == userId) {
            throw new ServiceException("PUBLIC-016");
        }
        SignIn signIn = new SignIn();
        signIn.setSignUserId(userId);
        return signInMapper.queryByUserId(signIn);
    }

    @Override
    public double signInDaysUpdate(Long signInId) throws Exception {
        if (null == signInId) {
            throw new ServiceException("SIGNIN-001");
        }
        double signInDays = (double) signInDetailsService.queryCountByHeadId(signInId);
        SignIn signIn = new SignIn();
        signIn.setId(signInId);
        signIn.setSignInDays(signInDays);
        signInMapper.signInDaysUpdate(signIn);
        return signInDays;
    }
}
